<?php

namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Input;
use \App\Admin\Item_sowingMap;
use \App\Admin\Item;
use zgldh\QiniuStorage\QiniuStorage;

class Item_sowing_mapController extends Controller
{
    public function getOne(){
        $item_id = Input::get('item_id');
        $res = Item_sowingMap::where('item_id',$item_id) -> first();
        return response() -> json($res);
    }

    public function edit(){
        $data = Input::all();
        $result = Item_sowingMap::where('item_id',$data['item_id']) -> update(['item_img' => $data['item_img']]);
        session(['sowing_map_img_url' => null]);
        return $result ? '1' : '0';
    }

    public function del(){
        $item_id = Input::get('item_id');
        $res = Item::where('id',$item_id) -> update(['is_sowing_map' => '0']);
        if($res){
            $url = Item_sowingMap::where('item_id',$item_id) -> value('item_img');
            //删除原有图片
            $disk = QiniuStorage::disk('qiniu_item');
            if($disk->exists($url) == true){
                $result = $disk->delete($url);
                if($result == false){
                    $data = array(
                        'status'	=>	'0',
                        'message'	=>	'删除原图片失败',
                    );
                    return response() -> json($data);
                }
            }
            $fres = Item_sowingMap::where('item_id',$item_id) -> delete();
            return $fres ? '1' : '0';
        }else{
            $data = array(
                'status'	=>	'0',
                'message'	=>	'修改状态为0失败',
            );
            return response() -> json($data);
        }
    }
}